<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo - Modules - Admin CP Overview
*
* @package		Ineo
* @subpackage   Modules
* @copyright	2006, Veriea
*/
class ineo_mod_acp_overview
{
	/**
	* Variable Setup
	*/
	var $ipsclass;
	var $core;
	
	/**
	* Initialize
	*/
	function module_init()
	{
		// ACP Module, Do nothing if not in ACP!
		if( !IN_ACP )
		{
			return false;
		}
	}
	
	/**
	* Default Function for this module:
	*/
	function module_default()
	{
		return $this->overview();
	}
	
	/**
	* Show ACP overview page:
	*/
	function overview()
	{
		// Set up page title and description:
		$this->ipsclass->admin->page_title = 'Ineo Overview';
		$this->ipsclass->admin->page_detail = 'An outline of important statistics and information for your Ineo installation.';
		
		// Version check!
		$check_image_location = 'http://www.veriea.com/scripts/versioncheck.php?product=ineo&siteversion='.$this->core->version['code'];
		$check_html = "<img src='{$check_image_location}' alt='Version Check' />";
		
		
		// Show some basic system statistics:
		$this->show_statistics();
		
		// Show the latest moderation logs:
		$this->show_moderator_logs();
		
		// Show the last 5 recently added and 5 recently updated articles:
		$this->show_recent_articles();
	}
	
	/**
	* Show statistics
	*/
	function show_statistics()
	{
		// Load version history:
		$this->ipsclass->DB->simple_construct(
												array( 
														'select' => '*',
														'from' => 'ineo_upgrade_history',
													 )
												);
		$this->ipsclass->DB->simple_order('ineo_upgrade_id DESC');
		$this->ipsclass->DB->simple_exec();
		
		$versions = '';
		
		while( $v = $this->ipsclass->DB->fetch_row() )
		{
			$date = $this->ipsclass->get_date($v['ineo_upgrade_date'],'');
			$versions .= "<tr><td class='{$this->core->api->row2}'>{$v['ineo_version_id']} - {$v['ineo_version_human']}</td><td class='{$this->core->api->row2}'>{$date}</td></tr>";
		}
		
		// Find out how many articles there are.
		$this->ipsclass->DB->simple_construct( array( 'select' => 'count(*) as cnt', 'from' => 'ineo_articles' ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();
		$art_count = $data['cnt'];
		
		// Now let's find out how many queued articles there are!
		$this->ipsclass->DB->simple_construct( array( 'select' => 'count(*) as cnt', 'from' => 'ineo_articles', 'where' => 'article_is_queued = 1' ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();
		$queued_art_count = $data['cnt'];
		
		// Find out how many total revisions there are. $rev_count will only equal non-active revisions, more or less. ;)
		$this->ipsclass->DB->simple_construct( array( 'select' => 'count(*) as cnt', 'from' => 'ineo_content' ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();
		$rev_count = $data['cnt'] - $art_count;
		
		// Now let's find out how many queued revisions there are!
		$this->ipsclass->DB->simple_construct( array( 'select' => 'count(*) as cnt', 'from' => 'ineo_content', 'where' => 'content_is_queued = 1' ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();
		$queued_rev_count = $data['cnt'];
		
		$this->ipsclass->html .= <<<OVERVIEW
		
		<div style='text-align: right'>{$check_html}</div>
		<br />
		<table class='ipbtable' style='width: 100%'>
			<tr>
				<td style='vertical-align: top; width: 50%'>
					<div class='{$this->core->api->cbox}'>
						<div class='{$this->core->api->ctitle}'>
							Statistics
						</div>
						
						<table class='ipbtable' style='width: 100%' cellspacing='0'>
							<tr>
								<td class='{$this->core->api->row2}'>
								<strong>Ineo Version</strong>
								</td>
								<td class='{$this->core->api->row2}'>
								{$this->core->version['human']}
								</td>
							</tr>
							<tr>
								<td class='{$this->core->api->row2}'>
								<b>Total Articles</b>
								</td>
								<td class='{$this->core->api->row2}'>
								{$art_count}
								</td>
							</tr>
							<tr>
								<td class='{$this->core->api->row2}'>
								<b>Non-active Revisions</b>
								</td>
								<td class='{$this->core->api->row2}'>
								{$rev_count}
								</td>
							</tr>
								<td class='{$this->core->api->row2}'>
								<b>Queued Articles</b>
								</td>
								<td class='{$this->core->api->row2}'>
								{$queued_art_count}
								</td>
							</tr>
								<td class='{$this->core->api->row2}'>
								<b>Queued Revisions</b>
								</td>
								<td class='{$this->core->api->row2}'>
								{$queued_rev_count}
								</td>
							</tr>
						</table>
					</div>
				</td>
				<td style='vertical-align: top;'>
					<div class='{$this->core->api->cbox}'>
						<div class='{$this->core->api->ctitle}'>
							Upgrade History
						</div>
						<table class='ipbtable' style='width: 100%' cellspacing='0'>
							<tr>
								<th class='{$this->core->api->thead}'>Version Information</th>
								<th class='{$this->core->api->thead}'>Date Installed</th>
							</tr>
							
							{$versions}
						</table>
					</div>
				</td>
			</tr>
		</table>
		
OVERVIEW;
	}
	
	/**
	* Show Moderator Logs
	*/
	function show_moderator_logs()
	{
		//-----
		// Load and show 5x moderator logs!
		//-----
		$this->ipsclass->DB->simple_construct( array('select' => 'l.*,m.members_display_name','from'=>array('ineo_logs' => 'l'), 'add_join' => array( 1=> array('type' => 'left','from' => array('members'=>'m'), 'where' => 'm.id = l.member_id'))) );
		$this->ipsclass->DB->simple_order('l.id DESC');
		$this->ipsclass->DB->simple_limit('5');
		$this->ipsclass->DB->simple_exec();

		$ROW = '';
		
		// loooooooooooooooooooop!
		while( $res = $this->ipsclass->DB->fetch_row() )
		{
			$date = $this->ipsclass->get_date($res['time'],'');
			$ROW .= "
			<tr>
				<td class='{$this->core->api->row1}'>
					{$res['note']}
				</td>
				<td class='{$this->core->api->row1}'>
					{$res['members_display_name']}
				</td>
				<td class='{$this->core->api->row1}'>
					{$date}
				</td>
			</tr>";
		}
		
		$this->ipsclass->html .= <<<UBERVOO
		<br />
			<div class='{$this->core->api->cbox}' style='margin: 2px;'>
				<div class='{$this->core->api->ctitle}'>
					Last 5 Moderator Actions
				</div>

				<table class='ipbtable' style='width: 100%' cellspacing='0'>
					<tr>
						<th class='{$this->core->api->thead}'>Action</th>
						<th class='{$this->core->api->thead}'>Member</th>
						<th class='{$this->core->api->thead}'>Date</th>
					</tr>
					{$ROW}
				</table>

			</div>
		<br />
UBERVOO;
	}
	
	/**
	* Show Recent Articles
	*/
	function show_recent_articles()
	{
		//-----
		// Load and show latest / recent updated articles:
		//-----
		
		$added   = $this->get_recent('article_id');
		$updated = $this->get_recent('article_edited_date');
		
		$this->ipsclass->html .= <<<OVERVIEW

		<table class='ipbtable' style='width: 100%'>
			<tr>
				<td style='vertical-align: top; width: 50%'>
					<div class='{$this->core->api->cbox}'>
						<div class='{$this->core->api->ctitle}'>
							Latest Added
						</div>

						<table class='ipbtable' style='width: 100%' cellspacing='0'>
							<tr>
								<th class='{$this->core->api->thead}'>Title</th>
								<th class='{$this->core->api->thead}'>Date</th>
							</tr>
							{$added}
						</table>
					</div>
				</td>
				<td style='vertical-align: top;'>
					<div class='{$this->core->api->cbox}'>
						<div class='{$this->core->api->ctitle}'>
							Recently Updated
						</div>
						<table class='ipbtable' style='width: 100%' cellspacing='0'>
							<tr>
								<th class='{$this->core->api->thead}'>Title</th>
								<th class='{$this->core->api->thead}'>Date</th>
							</tr>
							{$updated}
						</table>
					</div>
				</td>
			</tr>
		</table>

OVERVIEW;
	}
	
	/**
	* Load recent articles by their update or added date. Used only by the above function.
	*/
	function get_recent( $column_name )
	{
		// Query:
		$this->ipsclass->DB->simple_construct( array(
														'select' => 'article_id,article_title,article_edited_date',
														'from'   => 'ineo_articles',
														'where' => 'article_is_queued = \'0\'',
													)
											);
		$this->ipsclass->DB->simple_order( $column_name . ' DESC' );
		$this->ipsclass->DB->simple_limit( 5 );									
		$list = $this->ipsclass->DB->simple_exec();
		
		$articles = '';
		
		// Build list using the template:
		while( $res = $this->ipsclass->DB->fetch_row( $list ) )
		{
			// Make the date from timestamp:
			$res['article_date'] = $this->ipsclass->get_date($res['article_edited_date'],'');
			
			$articles .= "<tr><td class='{$this->core->api->row2}'>{$res['article_title']}</td><td class='{$this->core->api->row2}'>{$res['article_date']}</td></tr>";
		}
		
		return $articles;
	}
}

?>